<style>
.row {
    display: flex;
    align-items: center;
    padding: 5px 0;

    .row-center {
        width: 100%;
        display: flex;
        justify-content: center;
    }
}

.face-container {
    padding: 0 50px;
    background: white;
    min-height: calc(100vh - 200px);
    display: flex;
    flex-direction: column;

    .face_title {
        padding: 10px 0;
        border-bottom: #dddddd solid 1px;
        color: #bfda7e;
    }

    .upload-image {
        display: flex;
        padding: 100px 0;
        justify-content: center;
        align-items: center;
    }
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 50%;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader .hover-text {
    display: none;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: #409EFF;
    font-size: 16px;
    text-align: center;
}

.avatar-uploader .el-upload:hover .hover-text {
    display: block;
    background: rgba(0, 0, 0, 0.5);
    width: 100%;
    height: 100%;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
    border-radius: 50%;
}
</style>
<template>
    <div>
        <div class="face-container">
            <div class="face_title">我的头像</div>
            <div class="upload-image">
                <el-upload class="avatar-uploader" action="http://localhost:9090/upload/image" :show-file-list="false"
                    :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload" name="image">
                    <img v-if="form.avatar" :src="form.avatar" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    <div class="hover-text">上传头像</div>
                </el-upload>
            </div>
            <div class="row">
                <div class="row-center">
                    <el-button type="success" plain @click="save">保存</el-button>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import { jump } from '@/utils/commonFunction';
import request from '@/utils/request';
export default {
    data() {
        return {
            form: {
                avatar: ''
            }
        };
    },
    created() {
        this.form.avatar = JSON.parse(localStorage.getItem('userInfo')).avatar;
    },
    methods: {

        handleAvatarSuccess(res, file) {
            this.form.avatar = URL.createObjectURL(file.raw);
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },

        save() {
            if (!this.form.avatar) {
                this.$message({
                    message: '请上传新头像',
                    type: 'warning'
                });
                return
            }
            request.post("/sys_users", this.form).then(res => {
                if (res.code == 200) {
                    this.$message({
                        message: '保存成功',
                        type: 'success'
                    });
                    const userInfo = JSON.parse(localStorage.getItem('userInfo'));
                    userInfo.avatar = this.form.avatar;
                    localStorage.setItem('userInfo', JSON.stringify(userInfo));
                }
            })
            let self = this;
            setTimeout(function () {
                let userId = self.$route.params.userId;
                jump(`/personCenter/${userId}`)
            }, 1000);

        }
    }
}
</script>